package com.magic.smartcity.view.me;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.magic.smartcity.R;
import com.magic.smartcity.beans.UserBean;
import com.magic.smartcity.databinding.FragmentMeEditInfoBinding;
import com.magic.smartcity.interfaces.IUserPresenter;
import com.magic.smartcity.interfaces.base.BaseUserCallBackFragment;
import com.magic.smartcity.utils.Constant;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MeEditInfoFragment extends BaseUserCallBackFragment{


    private static final String TAG = "MeEditInfoFragment";
    private FragmentMeEditInfoBinding mBinding;
    private int sex;
    @Inject
    IUserPresenter mPresenter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mPresenter.registerCallBack(this);
        mBinding = FragmentMeEditInfoBinding.inflate(getLayoutInflater());
        setHasOptionsMenu(true);//！！！！！一定要调用此方法显示，否则无法启用optionsMenu

        initData();

        return mBinding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unRegisterCallBack(this);
    }

    private void initData() {
        if (Constant.TOKEN !=null)
            mPresenter.getNowUser();
    }

    ////////以下为创建标题栏按钮
    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.user_info_edit,menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.edit_user:
                mPresenter.updateUser(
                        mBinding.nickname.getText().toString(),
                        sex+"",
                        mBinding.phone.getText().toString(),
                        mBinding.userIdCard.getText().toString(),
                        mBinding.email.getText().toString());
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /////以下为接口回调实现
    @Override
    public void getUserLoaded(UserBean.UserBeanDo user) {
        if (user!=null) {
            mBinding.username.setText(user.getUserName());
            mBinding.nickname.setText(user.getNickName());
            if (Integer.parseInt(user.getSex())==0){
                mBinding.nan.setChecked(true);
            }else{
                mBinding.nv.setChecked(true);
            }
            mBinding.phone.setText(user.getPhonenumber());
            mBinding.sexGroup.setOnCheckedChangeListener((group, checkedId) -> {
                sex = checkedId==R.id.nan?0:1;
            });
            mBinding.userIdCard.setText(user.getIdCard());
            mBinding.email.setText(user.getEmail());
        }
    }

    @Override
    public void registerOk(String token) {
        Toast.makeText(getContext(), ""+token, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void error(String str) {
        Toast.makeText(getContext(), ""+str, Toast.LENGTH_SHORT).show();
    }
}